package org.nettyServer.server;

import java.util.concurrent.TimeUnit;
import io.netty.channel.Channel;
import io.netty.channel.ChannelInitializer;
import io.netty.channel.ChannelPipeline;
import io.netty.handler.codec.bytes.ByteArrayEncoder;
import io.netty.handler.timeout.IdleStateHandler;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;

/**
 * 类描述:TCP通道配置
 */
@Component
public class ServerChannelInitializer extends ChannelInitializer {

@Resource
private  ServerHandler serverHandler;



    @Override
    protected void initChannel(Channel ch) throws Exception {
        ChannelPipeline pipeline = ch.pipeline();
        //心跳检测
        pipeline.addLast(new IdleStateHandler(0,0 , 10, TimeUnit.SECONDS));
        pipeline.addLast(new IdleHandler());
        //字节数组编码
        pipeline.addLast(new ByteArrayEncoder());
        //字节数组解码（重写类，如不需要可使用默认）
        pipeline.addLast(new TCPBaseMessageDecoder());
        //客户端的逻辑
        pipeline.addLast("handler", serverHandler);
    }
}
